package com.yulebaby.callsenter.task;

import android.text.TextUtils;

import com.lzy.okgo.model.HttpParams;
import com.yulebaby.callsenter.bean.CallLogBean;
import com.yulebaby.callsenter.bean.ResultBean;
import com.yulebaby.callsenter.database.manager.Dao;
import com.yulebaby.callsenter.global.Constant;
import com.yulebaby.callsenter.http.HttpHelper;
import com.yulebaby.callsenter.http.ServerUrls;
import com.yulebaby.callsenter.http.callback.HttpCallback;
import com.yulebaby.callsenter.utils.SPUtil;
import com.yulebaby.callsenter.utils.ToastUtil;

import java.io.File;
import java.util.List;

/**
 * Author: Zhangbo
 * Time: 2016/11/18
 */
public class UploadCallLogTask {
    /**
     * 上传单条通话记录
     *
     * @param bean
     */
    public static void doUploadFiles(final CallLogBean bean) {
//        if (NetWorkUtil.isWifiConnected()) {  //取消wifi检测
        if (bean != null) {
            HttpParams httpParams = new HttpParams();
            String user_token = (String) SPUtil.get(Constant.USER_TOKEN, "");
            int user_id = (int) SPUtil.get(Constant.USER_ID, -1);
            httpParams.put("token", user_token);
            httpParams.put("sale_id", user_id);
            httpParams.put("customer_phone", bean.getCustomer_phone());
            httpParams.put("call_type", bean.getCall_type());
            httpParams.put("call_time", bean.getCall_time());
            httpParams.put("call_duration", bean.getCall_duration());
            if (!TextUtils.isEmpty(bean.getFile_path())) {
                httpParams.put("file", new File(bean.getFile_path()));
            }
            HttpHelper.getInstance().postFile(ServerUrls.OPT_UPLOAD, httpParams, new HttpCallback<ResultBean>() {
                @Override
                public void onSuccessResultNull() {
                    super.onSuccessResultNull();
                    ToastUtil.showToast("成功");
                    //上传成功后对成功文件打上传成功的标记
                    bean.setIsUpdate(1);
                    Dao.getInstance().updateBean(bean);
                }

                @Override
                public void onFailParam(String info) {
                    super.onFailParam(info);
                }

                @Override
                public void onFail(String exception) {
                    super.onFail(exception);
                }

            });
        }
    }
//    }

    /**
     * 上传多条通话记录
     *
     * @param list
     */
    public static void doUploadListFiles(List<CallLogBean> list) {
        if (list != null) {
            for (CallLogBean callLogBean : list) {
                UploadCallLogTask.doUploadFiles(callLogBean);
            }
        }
    }

}
